<template>
  <div class="app-container">
    <div class="Search">
      <!--工具条-->
      <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
        <el-form :inline="true" :model="query">
          <el-form-item v-has="'PLATFROM_TENANT_QUERY'">
            <el-cascader ref="tenantTree" placeholder="所属租户" :props="tenantProps" :options="tenantOptions"
              @change="selectTenant" clearable collapse-tags filterable></el-cascader>
          </el-form-item>
          <el-form-item>
            <el-cascader ref="organTree" placeholder="所属组织" :props="props" :options="organOptions" @change="selectOrgan"
              collapse-tags clearable filterable></el-cascader>
          </el-form-item>
          <el-form-item>
            <el-input v-model="query.params.name" placeholder="规格组名称" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="fetchData">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="success" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </div>
    <div class="block">
      <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row>
        <el-table-column align="center" label="序号" width="95">
          <template slot-scope="scope">
            {{ scope.$index }}
          </template>
        </el-table-column>
        <el-table-column label="规格组名称" width="110">
          <template slot-scope="scope">
            {{ scope.row.name }}
          </template>
        </el-table-column>
        <el-table-column label="所属组织" width="110">
          <template slot-scope="scope">
            {{ scope.row.organId }}
          </template>
        </el-table-column>
        <el-table-column label="组织路径" width="110">
          <template slot-scope="scope">
            {{ scope.row.organPath }}
          </template>
        </el-table-column>
        <el-table-column label="备注" width="110">
          <template slot-scope="scope">
            {{ scope.row.remark }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="created_at" label="操作">
          <template slot-scope="scope">
            <el-link  type="warning" icon="el-icon-edit" style="cursor: pointer;"
              @click="handleEdit(scope.$index, scope.row)">
              修改
            </el-link>
            <el-link  type="danger"  icon="el-icon-delete" @click="handleDelete(scope.$index, scope.row)">
              删除
            </el-link>
            <el-link type="primary"  icon="el-icon-reading"
              @click="handleShowGoodsSpecificaionList(scope.$index, scope.row)">
              显示商品规格
            </el-link>
            <el-link v-has="'DICTIONARYORGAN_ASSIGNEDDICTIONARY'" type="info" icon="el-icon-share"
              @click="handleShowAssignGroup2OrganShowOrganList(scope.$index, scope.row)">
              指派给
            </el-link>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="block">
      <el-pagination :current-page="currentPage" :page-sizes="[10, 20, 30, 40]" :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
        @current-change="handleCurrentChange" />
    </div>
    <div class="block">
      <!--弹框-->
      <el-dialog :title="titleMap[dialogStatus]" :visible.sync="dialogFormVisible">
        <el-form :model="form">
          <el-form-item v-if="addForVisible" label="主键">
            <el-input v-model="form.id" placeholder="主键" disabled auto-complete="off" />
          </el-form-item>
          <el-form-item label="规格组名称">
            <el-input v-model="form.name" placeholder="规格组名称" auto-complete="off" />
          </el-form-item>
          <el-form-item v-has="'PLATFROM_TENANT_QUERY'" label="所属租户" placeholder="所属租户" required>
            <el-cascader v-model="form.tenantId" ref="tenantTreeForm" placeholder="所属租户" :props="tenantProps"
              :options="tenantOptions" @change="chooseTenantForm" style="width:100%" clearable collapse-tags
              filterable></el-cascader>
          </el-form-item>
          <el-form-item label="所属组织">
            <el-cascader v-model="form.organId" ref="organTreeForm" placeholder="所属组织" :props="props"
              :options="organFormOptions" @change="chooseOrganForm" style="width:100%" collapse-tags
              filterable></el-cascader>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.remark" placeholder="备注" auto-complete="off" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleEditSave">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <div class="block">
      <!--弹框-->
      <el-dialog :title="titleMap[dialogStatus]" width="78%" :visible.sync="dialogFormShowGoodsSpecificationVisible">
        <div class="Search">
          <!--工具条-->
          <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="query">
              <el-form-item>
                <el-input v-model="query.goodsSpecification.parameterName" placeholder="参数名称" auto-complete="off" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="handleQueryGoodsTypeSpecification">查询</el-button>
              </el-form-item>
              <el-form-item>
                <el-button type="success" @click="handleAddGoodsTypeSpecification">添加商品分类规格</el-button>
              </el-form-item>
            </el-form>
          </el-col>
        </div>
        <el-table v-loading="listLoading" :data="goodsSpecificationList" element-loading-text="Loading" border fit
          highlight-current-row @expand-change="expandChange">
          <el-table-column type="expand" align="center" label="" width="58">
            <template slot-scope="scope">
              <el-table v-loading="listLoading" :data="goodsTypeSpecificationItemList" element-loading-text="Loading"
                border fit highlight-current-row>
                <el-table-column align="center" label="序号" width="58">
                  <template slot-scope="scope">
                    {{ scope.$index }}
                  </template>
                </el-table-column>
                <el-table-column label="分类属性值" width="110">
                  <template slot-scope="scope">
                    {{ scope.row.specificationValue }}
                  </template>
                </el-table-column>
                <el-table-column label="备注">
                  <template slot-scope="scope">
                    {{ scope.row.remark }}
                  </template>
                </el-table-column>
              </el-table>
              <div class="block">
                <el-pagination :current-page="query.goodsSpecificationItem.currentPage" :page-sizes="[10, 20, 30, 40]"
                  :page-size="query.goodsSpecificationItem.pageSize" layout="total, sizes, prev, pager, next, jumper"
                  :total="query.goodsSpecificationItem.total" @size-change="handleQueryItemValSizeChange"
                  @current-change="handleQueryItemCurrentChange" />
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="序号" width="95">
            <template slot-scope="scope">
              {{ scope.$index }}
            </template>
          </el-table-column>
          <el-table-column label="规格名称" width="110">
            <template slot-scope="scope">
              {{ scope.row.parameterName }}
            </template>
          </el-table-column>
          <el-table-column label="规格代码" width="110">
            <template slot-scope="scope">
              {{ scope.row.parameterCode }}
            </template>
          </el-table-column>
          <el-table-column label="显示区域" width="110">
            <template slot-scope="scope">
              {{ scope.row.displayArea }}
            </template>
          </el-table-column>
          <el-table-column label="商品分类编号" width="110">
            <template slot-scope="scope">
              {{ scope.row.goodsTypeId }}
            </template>
          </el-table-column>
          <el-table-column label="规格组编号" width="110">
            <template slot-scope="scope">
              {{ scope.row.groupId }}
            </template>
          </el-table-column>
          <el-table-column label="备注" width="110">
            <template slot-scope="scope">
              {{ scope.row.remark }}
            </template>
          </el-table-column>
          <el-table-column align="center" prop="created_at" label="操作">
            <template slot-scope="scope">
              <span class="el-tag el-tag--info el-tag--mini" style="cursor: pointer;"
                @click="handleModifyGoodsTypeSpecification(scope.$index, scope.row)">
                修改
              </span>
              <span class="el-tag el-tag--danger el-tag--mini" style="cursor: pointer;"
                @click="handleRemoveGoodsTypeSpecification(scope.$index, scope.row)">
                删除
              </span>
              <span class="el-tag el-tag--danger el-tag--mini" style="cursor: pointer;"
                @click="handleAddGoodsTypeSpecificationItem(scope.$index, scope.row)">
                添加规格项
              </span>
              <el-link v-has="'DICTIONARYORGAN_ASSIGNEDDICTIONARY'" type="info" style="cursor: pointer;"
                @click="handleShowAssignSpecification2OrganList(scope.$index, scope.row)">
                指派给
              </el-link>
            </template>
          </el-table-column>
        </el-table>
        <div class="block">
          <el-pagination :current-page="query.goodsSpecification.currentPage" :page-sizes="[10, 20, 30, 40]"
            :page-size="query.goodsSpecification.pageSize" layout="total, sizes, prev, pager, next, jumper"
            :total="query.goodsSpecification.total" @size-change="handleQueryGoodsSpecificationSizeChange"
            @current-change="handleQueryGoodsSpecificationCurrentChange" />
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormShowGoodsSpecificationVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleGoodsSpecification2OrganSave">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <div class="block">
      <!--弹框-->
      <el-dialog :title="titleMap[dialogStatus]" :visible.sync="dialogFormEditGoodsTypeSpecificationVisible">
        <el-form :model="form">
          <el-form-item v-if="addForGoodsTypeSpecificationVisible" label="主键">
            <el-input v-model="goodsSpecification.id" placeholder="主键" auto-complete="off" />
          </el-form-item>
          <el-form-item label="规格组编号">
            <el-input v-model="goodsSpecification.groupId" placeholder="规格组编号" auto-complete="off" />
          </el-form-item>
          <el-form-item label="显示区域">
            <el-select v-model="goodsSpecification.displayArea" placeholder="显示区域" style="width: 100%">
              <el-option v-for="item in query.displayAreaOptions" :key="item.value" :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="规格名称">
            <el-input v-model="goodsSpecification.parameterName" placeholder="规格名称" auto-complete="off" />
          </el-form-item>
          <el-form-item label="规格代码">
            <el-input v-model="goodsSpecification.parameterCode" placeholder="规格代码" auto-complete="off" />
          </el-form-item>
          <el-form-item label="商品分类">
            <el-cascader v-model="goodsSpecification.goodsTypeId" ref="goodsTypeTree" placeholder="请选择商品分类"
              :props="goodsTypeTreeProps" :options="goodsTypeTreeOptionData" @change="getGoodsType(goodsSpecification)"
              collapse-tags style="width: 100%" filterable></el-cascader>
          </el-form-item>
          <el-form-item label="是否必选">
            <el-radio-group v-model="goodsSpecification.isMust">
              <el-radio :label="true">必选</el-radio>
              <el-radio :label="false">可选</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="是否多选">
            <el-radio-group v-model="goodsSpecification.isMutilSelect">
              <el-radio :label="true">多选</el-radio>
              <el-radio :label="false">单选</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="是否导航">
            <el-radio-group v-model="goodsSpecification.isNav">
              <el-radio :label="true">是</el-radio>
              <el-radio :label="false">否</el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item label="备注">
            <el-input v-model="form.remark" placeholder="备注" auto-complete="off" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormEditGoodsTypeSpecificationVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleEditGoodsSpecificationSave">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <div class="block">
      <!--弹框-->
      <el-dialog :title="titleMap[dialogStatus]" :visible.sync="dialogFormEditGoodsTypeSpecificationItemVisible">
        <el-form :model="form">
          <el-form-item v-if="addGoodsTypeSpecificationItemForVisible" label="主键">
            <el-input v-model="goodsSpecificationItem.id" placeholder="主键" auto-complete="off" />
          </el-form-item>
          <el-form-item label="关联规格">
            <el-input v-model="goodsSpecificationItem.specificationId" placeholder="关联规格" auto-complete="off" />
          </el-form-item>
          <el-form-item label="规格值">
            <el-input v-model="goodsSpecificationItem.specificationValue" placeholder="规格值" auto-complete="off" />
          </el-form-item>
          <el-form-item label="商品分类">
            <el-cascader ref="goodsTypeTree" placeholder="请选择商品分类" :props="goodsTypeTreeProps"
              :options="goodsTypeTreeOptionData" @change="getGoodsType(goodsSpecificationItem)" collapse-tags
              style="width: 100%" filterable></el-cascader>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="goodsSpecificationItem.remark" placeholder="备注" auto-complete="off" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormEditGoodsTypeSpecificationItemVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleEditGoodsTypeSpecificationItemSave">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <div class="block">
      <!--弹框-->
      <el-dialog :title="titleMap[dialogStatus]" :visible.sync="dialogFormAssignGroup2OrganVisible">
        <el-table v-loading="listLoading" :data="organList" element-loading-text="Loading" border fit
          highlight-current-row @selection-change="handleOrganSelectionChange" row-key="id">
          <el-table-column type="selection" width="55" />
          <el-table-column align="center" label="序号" width="95">
            <template slot-scope="scope">
              {{ scope.$index }}
            </template>
          </el-table-column>
          <el-table-column label="组织名称" width="220">
            <template slot-scope="scope">
              {{ scope.row.organName }}
            </template>
          </el-table-column>
          <!--  <el-table-column label="父组织编号" width="120" >
                      <template slot-scope="scope">
                        {{ scope.row.parentId }}
                      </template>
                    </el-table-column> -->
          <el-table-column label="状态" width="120" prop="status" :formatter="statusFormat" />
          <el-table-column label="类型" width="120" prop="type" :formatter="typeFormat" />
          <!--  <el-table-column label="组织层次路径" width="110">
                      <template slot-scope="scope">
                        {{ scope.row.organPath }}
                      </template>
                    </el-table-column> -->
          <el-table-column label="备注" width="210">
            <template slot-scope="scope">
              {{ scope.row.remark }}
            </template>
          </el-table-column>
        </el-table>
        <div class="block">
          <el-pagination :current-page="query.organ.currentPage" :page-sizes="[10, 20, 30, 40]"
            :page-size="query.organ.pageSize" layout="total, sizes, prev, pager, next, jumper"
            :total="query.organ.total" @size-change="handleOrganSizeChange"
            @current-change="handleOrganCurrentChange" />
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormAssignGroup2OrganVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleAssignGoodsSpecificationGroup2OrganSave">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <div class="block">
      <!--弹框-->
      <el-dialog :title="titleMap[dialogStatus]" :visible.sync="dialogFormAssignSpecificationOrganVisible">
        <el-table v-loading="listLoading" :data="organList" element-loading-text="Loading" border fit
          highlight-current-row @selection-change="handleOrganSelectionChange" row-key="id">
          <el-table-column type="selection" width="55" />
          <el-table-column align="center" label="序号" width="95">
            <template slot-scope="scope">
              {{ scope.$index }}
            </template>
          </el-table-column>
          <el-table-column label="组织名称" width="220">
            <template slot-scope="scope">
              {{ scope.row.organName }}
            </template>
          </el-table-column>
          <!--  <el-table-column label="父组织编号" width="120" >
                      <template slot-scope="scope">
                        {{ scope.row.parentId }}
                      </template>
                    </el-table-column> -->
          <el-table-column label="状态" width="120" prop="status" :formatter="statusFormat" />
          <el-table-column label="类型" width="120" prop="type" :formatter="typeFormat" />
          <!--  <el-table-column label="组织层次路径" width="110">
                      <template slot-scope="scope">
                        {{ scope.row.organPath }}
                      </template>
                    </el-table-column> -->
          <el-table-column label="备注" width="210">
            <template slot-scope="scope">
              {{ scope.row.remark }}
            </template>
          </el-table-column>
        </el-table>
        <div class="block">
          <el-pagination :current-page="query.organ.currentPage" :page-sizes="[10, 20, 30, 40]"
            :page-size="query.organ.pageSize" layout="total, sizes, prev, pager, next, jumper"
            :total="query.organ.total" @size-change="handleOrganSizeChange"
            @current-change="handleOrganCurrentChange" />
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormAssignSpecificationOrganVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleGoodsSpecification2OrganSave">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getList, doDelete, doEdit, assign2Organs as assignGoodsSpecificationGroup2Organ } from '@/api/GoodsSpecificationGroup/'
import { getList as queryGoodsTypeSpecification, doDelete as deleteGoodsTypeSpecification, doEdit as saveGoodsTypeSpecification, assign2Organs as assignGoodsSpecification2Organ } from '@/api/GoodsSpecification/'
import { getList as queryGoodsTypeSpecificationItem, doDelete as deleteGoodsTypeSpecificationItem, doEdit as saveGoodsTypeSpecificationItem } from '@/api/GoodsSpecificationItem/'
import { getGoodsTypeTree } from '@/api/GoodsType'
import { getList as getOrganPagerList, querySubordinateOrgan } from '@/api/Organ'
import { getDictionaryGroup, getReferencedDictionaryList } from '@/api/dictionary'
import { getTenantList, getOrganList, getDictionary, translateOrganId2OrganName } from '@/utils/auth'
import localStorageUtil from '@/utils/localStorageUtil'
import { getOrganId, getTenantId, getOrganPath, getCurrentLoginUserId, handleBuildTreeDataStruct, getSuperManagerId, getSelectedNodes, getSelectedNodesObject, getSystemErrorCode, getCurrentTimeYYYY_MM_dd_HH_mm_ss } from '@/api/utils'


export default {
  data() {
    return {
      list: [],
      listLoading: true,
      total: 5,
      currentPage: 1,
      pageSize: 10,
      dialogFormVisible: false,
      addForVisible: false,
      dialogFormShowGoodsSpecificationVisible: false,
      dialogFormAddGoodsSpecificationVisible: false,
      dialogFormEditGoodsTypeSpecificationVisible: false,
      addForGoodsTypeSpecificationVisible: false,
      dialogFormEditGoodsTypeSpecificationItemVisible: false,
      addGoodsTypeSpecificationItemForVisible: false,
      dialogFormAssignGroup2OrganVisible: false,
      dialogFormAssignSpecificationOrganVisible: false,
      goodsSpecificationList: [],
      goodsTypeSpecificationItemList: [],
      organTree: [],
      organList: [],
      form: {
        id: '',
        name: '',
        organId: '',
        organIds: [],
        organPath: '',
        isDeleted: '',
        createBy: '',
        createDate: '',
        updateBy: '',
        updateDate: '',
        remark: ''
      },
      goodsSpecification: {
        id: '',
        groupId: '',
        displayArea: '',
        parameterName: '',
        goodsTypeId: '',
        organId: '',
        organPath: '',
        isDeleted: '',
        createBy: '',
        createDate: '',
        updateBy: '',
        updateDate: '',
        remark: '',

      },
      goodsTypeSpecification: {
          isMust: null,
          isMutilSelect: null,
          isNav: null
      },
      goodsSpecificationGroup: {
        id: '',
        name: '',
        organId: '',
        organIds: [],
        organPath: '',
        isDeleted: '',
        createBy: '',
        createDate: '',
        updateBy: '',
        updateDate: '',
        remark: ''
      },
      goodsSpecificationItem: {
        id: '',
        specificationId: '',
        specificationValue: '',
        organId: '',
        organPath: '',
        isDeleted: '',
        createBy: '',
        createDate: '',
        updateBy: '',
        updateDate: '',
        remark: ''
      },
      assign2Organs: {
        assignees: [],
        id: '',
        createBy: '',
        updateBy: ''
      },
      assignGoodsSpecification2Organs: {
        assignees: [],
        id: '',
        createBy: '',
        updateBy: ''
      },
      // 新增or编辑弹框标题(根据点击的新增or编辑按钮显示不同的标题)
      titleMap: {
        add: '新增',
        edit: '编辑',
        showGoodsSpecification: '商品分类规格',
        editGoodsSpecification: '编辑商品分类规格',
        assignOrgan: '选择需要指派的组织'
      },
      dialogStatus: '',
      goodsTypeTreeOptionData: [],
      // 新增和编辑弹框标题
      clearable: true, // 可清空（可选）
      accordion: true, // 可收起（可选）
      goodsTypeValueId: null, // 初始ID（可选）
      props: { // 配置项（必选）
        value: 'id',
        label: 'organName',
        children: 'children',
        title: '组织',
        multiple: false,
        checkStrictly: true,
        emitPath: false
        // disabled:true
      },
      organTreeProps: {
        children: 'children',
        label: 'organName',
        url: 'url'
      },
      tenantProps: { // 配置项（必选）
        value: 'id',
        label: 'organName',
        children: 'children',
        title: '租户',
        multiple: false,
        checkStrictly: true,
        emitPath: false
        // disabled:true
      },
      supplierProps: { // 配置项（必选）
        value: 'id',
        label: 'organName',
        children: 'children',
        title: '租户',
        multiple: false,
        checkStrictly: true,
        emitPath: false
        // disabled:true
      },
      organFormOptions: [],
      tenantOptions: [],
      organOptions: [{ id: '1', parentId: '2', organName: '平台', children: [] }],
      supplierOptions: [{ id: '1', parentId: '2', organName: '平台', children: [] }],
      organQueryModel: 'selfAndSub',
      supplier: null, //供应商
      goodsType: null, //商品分类
      // // 新增or编辑弹框标题(根据点击的新增or编辑按钮显示不同的标题)
      optionData: [{ id: '1', parentId: '2', organName: '平台', children: [] }],
      goodsTypeTreeProps: { // 配置项（必选）
        value: 'id',
        label: 'typeName',
        children: 'children',
        title: '商品分类',
        multiple: false,
        emitPath: false
        // disabled:true
      },
      // 新增or编辑弹框标题(根据点击的新增or编辑按钮显示不同的标题)
      goodsTypeTreeOptionData: [{ id: '1', parentId: '2', organName: '平台', children: [] }],
      query: {
        params: {
          id: '',
          name: '',
          organId: '',
          organPath: '',
          isDeleted: '',
          createBy: '',
          createDate: '',
          updateBy: '',
          updateDate: '',
          remark: ''
        },
        goodsSpecification: {
          id: '',
          groupId: '',
          displayArea: '',
          parameterName: '',
          goodsTypeId: '',
          organId: '',
          organPath: '',
          isDeleted: '',
          createBy: '',
          createDate: '',
          updateBy: '',
          updateDate: '',
          remark: '',
          total: 5,
          currentPage: 1,
          pageSize: 10
        },
        goodsSpecificationItem: {
          specificationId: '',
          total: 5,
          currentPage: 1,
          pageSize: 10
        },
        organ: {
          organPath: '',
          organId: '',
          organName: '',
          pageSize: 10,
          total: 0,
          currentPage: 1
        },
        timeValue: '',
        options: [{
          value: '1',
          label: '黄金糕'
        }, {
          value: '2',
          label: '双皮奶'
        }],
        roleTypeOptions: [],
        typeOptions: [],
        organTypeOptions: [],
        statusOptions: [{
          value: '0',
          label: '未启用'
        }, {
          value: '1',
          label: '启用'
        }],
        displayAreaOptions: [],
        value: ''
      }
    }
  },
  created() {
    this.initOrganTree(() => {
      this.setOrganQueryType()
    }, getOrganPath())
    this.initOrganFormTree()
    this.handleFiltTenant()
    this.handleFiltSupplier()
    this.initGoodsTypeTree()
    this.handleBuildGoodsTypeSpecificationDisplayAreaOptions()
    this.fetchData()
  },
  methods: {
    displayAreaFormater(row, column) {
      let label = ''
      for (let i = 0; i < this.query.displayAreaOptions.length; i++) {
        if ('' + row.displayArea === this.query.displayAreaOptions[i].value) {
          return this.query.displayAreaOptions[i].label
        }
      }
      return label
    },
    statusFormat(row, column) {
      let label = ''
      for (let i = 0; i < this.query.statusOptions.length; i++) {
        if ('' + row.status === this.query.statusOptions[i].value) {
          return this.query.statusOptions[i].label
        }
      }
      return label

    },
    organTypeFormater(row, column) {
      let label = ''
      for (let i = 0; i < this.query.typeOptions.length; i++) {
        if ('' + row.organType === this.query.typeOptions[i].value) {
          return this.query.typeOptions[i].label
        }
      }
      return label
    },
    typeFormat(row, column) {

      let label = ''
      for (let i = 0; i < this.query.typeOptions.length; i++) {
        if ('' + row.type === this.query.typeOptions[i].value) {
          return this.query.typeOptions[i].label
        }
      }
      return label
    },
    fetchData() {
      this.listLoading = true
      getList(this.query.params, this.currentPage, this.pageSize).then(response => {
        const data = response.data
        this.total = data.totalCount
        this.pageSize = data.pageSize
        this.currentPage = data.currentPage
        this.list = data.entities
        this.listLoading = false
      })
    },
    createOrganQuery() {
      // 选中了租户
      let tenantSelected = this.$refs['tenantTree'] && this.$refs['tenantTree'].getCheckedNodes() || []
      let organSelected = this.$refs['organTree'] && this.$refs['organTree'].getCheckedNodes() || []
      let that = this
      this.query.params.organId = null
      if (tenantSelected.length && tenantSelected[0]) {
        this.query.params.tenantId = tenantSelected[0].data.id
        if (organSelected.length && organSelected[0]) {
          if (this.props.multiple) {
            this.query.params.organId = null
            this.query.params.organIds = []
            if (organSelected.length && organSelected[0]) {
              organSelected.forEach(function (element) {
                that.query.params.organIds.push(element.data.id)
              })
            }
          } else if (organSelected.length && organSelected[0]) {
            this.query.params.organPath = organSelected[0] && organSelected[0].data.organPath
          }
        }
      } else {
        this.query.params.tenantId = getTenantId()
        if (organSelected.length && organSelected[0]) {
          if (this.props.multiple) {
            this.query.params.organId = null
            this.query.params.organIds = []
            if (organSelected.length && organSelected[0]) {
              organSelected.forEach(function (element) {
                that.query.params.organIds.push(element.data.id)
              })
            }
          } else if (organSelected.length && organSelected[0]) {
            this.query.params.organPath = organSelected[0].data.organPath
          }
        } 
      }
      this.query.params.organId =  this.query.params.organId ||  getOrganId()
      this.query.params.organPath = this.query.params.organPath || getOrganPath()
    },
    initOrganTree() {
      this.listLoading = true
      querySubordinateOrgan({ organId: getOrganId() }).then(response => {
        console.log(response)
        const data = response.data.data
        this.optionData = handleBuildTreeDataStruct(data, 'id', 'parentId')
        this.listLoading = false
      })
    },
    handleSizeChange(val) {
      this.currentPage = 1
      this.pageSize = val
    },
    handleCurrentChange(val) {
      this.currentPage = val
    },
    // 新增
    handleAdd() {
      // 显示弹框
      this.dialogFormVisible = true
      // 新增弹框标题
      this.dialogStatus = 'add'

      this.form.id = ''
      this.form.name = ''
      this.form.organId = getOrganId()
      this.form.organPath = getOrganPath()
      this.form.tenantId = getTenantId()
      this.form.isDeleted = ''
      this.form.createBy = getCurrentLoginUserId()
      this.form.createDate = getCurrentTimeYYYY_MM_dd_HH_mm_ss()
      this.form.updateBy = ''
      this.form.updateDate = ''
      this.form.remark = ''
      this.addForVisible = false
    },
    // 编辑
    handleEdit(index, row) {
      console.log(index)
      console.log(row)
      this.form.id = row.id
      this.form.name = row.name
      this.form.organId = row.organId || getOrganId()
      this.form.organPath = row.organPath || getOrganPath()
      this.form.createBy = row.createBy
      this.form.createDate = row.createDate
      this.form.updateBy = getCurrentLoginUserId()
      this.form.updateDate = getCurrentTimeYYYY_MM_dd_HH_mm_ss()
      this.form.remark = row.remark
      this.addForVisible = true
      // 显示弹框
      this.dialogFormVisible = true
      // 编辑弹框标题
      this.dialogStatus = 'edit'
    },
    handleShowGoodsSpecificaionList(index, row) {

      this.dialogFormShowGoodsSpecificationVisible = true
      this.goodsSpecification.groupId = row.id
      this.query.goodsSpecification.groupId = row.id
      this.query.goodsSpecification.organId = row.organId || getOrganId()
      this.query.goodsSpecification.organPath = row.organPath || getOrganPath()
      this.query.goodsSpecification.tenantId = row.tenantId || getTenantId()
      this.goodsSpecificationGroup = row
      this.dialogStatus = 'showGoodsSpecification'
      this.handleQueryGoodsTypeSpecification()
    },
    handleAddGoodsTypeSpecification() {

      this.dialogFormEditGoodsTypeSpecificationVisible = true
      this.addForGoodsTypeSpecificationVisible = false
      this.dialogStatus = 'editGoodsSpecification'
      this.goodsSpecification.id = ''
      this.goodsSpecification.organId = this.goodsSpecification.organId || getOrganId()
      this.goodsSpecification.organPath = this.goodsSpecification.organPath || getOrganPath()
      this.goodsSpecification.createBy = getCurrentLoginUserId()
      this.goodsSpecification.createDate = getCurrentTimeYYYY_MM_dd_HH_mm_ss()
      this.goodsSpecification.updateBy = getCurrentLoginUserId()
      this.goodsSpecification.updateDate = getCurrentTimeYYYY_MM_dd_HH_mm_ss()
      debugger

    },
    handleModifyGoodsTypeSpecification(index, row) {

      this.goodsSpecification.groupId = row.groupId
      this.goodsSpecification.displayArea = row.displayArea
      this.goodsSpecification.parameterName = row.parameterName
      this.goodsSpecification.goodsTypeId = row.goodsTypeId
      this.goodsSpecification.organId = row.organId
      this.goodsSpecification.organPath = row.organPath
      this.goodsSpecification.isDeleted = row.isDeleted
      this.goodsSpecification.createBy = row.createBy
      this.goodsSpecification.createDate = row.createDate
      this.goodsSpecification.updateBy = row.updateBy
      this.goodsSpecification.updateDate = row.updateDate
      this.goodsSpecification.remark = row.remark
      this.goodsSpecification.id = row.id

      this.dialogFormEditGoodsTypeSpecificationVisible = true
      this.addForGoodsTypeSpecificationVisible = false
      this.dialogStatus = 'editGoodsSpecification'
    },
    handleQueryGoodsTypeSpecification() {
      this.listLoading = true
      this.query.goodsSpecification.currentPage = 1
      this.query.goodsSpecification.pageSize = 10
      queryGoodsTypeSpecification(this.query.goodsSpecification,
        this.query.goodsSpecification.currentPage,
        this.query.goodsSpecification.pageSize
      ).then(response => {
        const pager = response.data
        this.goodsSpecificationList = pager.entities
        this.query.goodsSpecification.pageSize = pager.pageSize
        this.query.goodsSpecification.total = pager.totalCount
        this.query.goodsSpecification.currentPage = pager.currentPage
        this.listLoading = false
      })

    },
    handleQueryGoodsSpecificationSizeChange(val) {

      this.query.goodsSpecification.currentPage = 1
      this.query.goodsSpecification.pageSize = val
      this.handleQueryGoodsTypeSpecification();
    },
    handleQueryGoodsSpecificationCurrentChange(val) {

      this.query.goodsSpecification.currentPage = val
      this.handleQueryGoodsTypeSpecification();

    },
    handleEditGoodsSpecificationSave() {

      this.listLoading = true
      this.goodsSpecification.organId = getOrganId()
      this.goodsSpecification.organPath = getOrganPath()
      this.goodsSpecification.tenantId = getTenantId()
      debugger

      saveGoodsTypeSpecification(this.goodsSpecification).then(response => {
        const data = response.data
        console.log(data)
        if (data.successful) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.handleQueryGoodsTypeSpecification()
          this.listLoading = false
        } else {
          this.$message({
            type: 'info',
            message: data.description || getSystemErrorCode(data.status)
          })
        }
        this.listLoading = false

      })
    },
    handleDelete(index, row) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        doDelete(row.id).then(response => {
          const data = response.data
          console.log(data)
          if (data.status == 1) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
            this.fetchData();
          } else if (data.status == 403) {
            this.$message({
              type: 'warning',
              message: data.description
            })
          } else {
            this.$message({
              type: 'info',
              message: '操作失败!'
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleRemoveGoodsTypeSpecification(index, row) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        deleteGoodsTypeSpecification(row.id).then(response => {
          const data = response.data
          if (data.successful) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
            this.fetchData();
          } else {
            this.$message({
              type: 'info',
              message: data.description || getSystemErrorCode(data.status)
            })
          }

        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleEditSave() {
      debugger

      doEdit(this.form).then(response => {
        debugger
        console.log(response)
        const data = response.data
        if (data.status) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.fetchData();
        } else {
          this.$message({
            type: 'info',
            message: '操作失败!'
          })
        }
        this.listLoading = false
        this.dialogFormVisible = false
      })
    },
    handleReset() {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleBuildGoodsTypeSpecificationDisplayAreaOptions() {

      let organId = getOrganId()

      getDictionaryGroup({ sys: 1, bigType: "GoodsTypeSpecificationDisplayArea", organId: getOrganId() }, organId).then(response => {
        const data = response.data
        let displayAreaOptions = [];
        for (let i = 0; i < data.length; i++) {

          let option = {}
          option['value'] = data[i].value
          option['label'] = data[i].desc
          displayAreaOptions.push(option)
        }
        this.query.displayAreaOptions = displayAreaOptions
      })
    },
    handleQueryItemValSizeChange(val) {

      this.query.goodsSpecificationItem.pageSize = val
      this.query.goodsSpecificationItem.currentPage = 1
      this.handleQueryGoodsTypeSpecificationItem()
    },
    handleQueryItemCurrentChange(val) {

      this.query.goodsSpecificationItem.currentPage = val
      this.handleQueryGoodsTypeSpecificationItem()
    },
    handleQueryGoodsTypeSpecificationItem() {

      queryGoodsTypeSpecificationItem(this.query.goodsSpecificationItem,
        this.query.goodsSpecificationItem.currentPage,
        this.query.goodsSpecificationItem.pageSize).then(response => {
          console.log(response)
          const pager = response.data

          this.query.goodsSpecificationItem.pageSize = pager.pageSize
          this.query.goodsSpecificationItem.currentPage = pager.currentPage
          this.query.goodsSpecificationItem.total = pager.totalCount
          this.goodsTypeSpecificationItemList = pager.entities


        })
    },
    handleAddGoodsTypeSpecificationItem(index, row) {

      this.dialogFormEditGoodsTypeSpecificationItemVisible = true
      this.addGoodsTypeSpecificationItemForVisible = false
      this.goodsSpecificationItem.specificationId = row.id


    },
    handleEditGoodsTypeSpecificationItemSave() {
      this.goodsSpecificationItem.organId = getOrganId()
      this.goodsSpecificationItem.organPath = getOrganPath()
      saveGoodsTypeSpecificationItem(this.goodsSpecificationItem).then(response => {
        debugger
        console.log(response)
        const data = response.data
        if (data.status) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.fetchData();
        } else {
          this.$message({
            type: 'info',
            message: data.description || getSystemErrorCode(data.status)
          })
        }
        this.listLoading = false
        this.handleQueryGoodsTypeSpecificationItem()
      })
    },
    initGoodsTypeTree() {
      this.listLoading = true
      getGoodsTypeTree({ organId: getOrganId(),tenantId: getTenantId() }).then(response => {
        console.log(response)
        const data = response.data.data
        this.goodsTypeTreeOptionData = handleBuildTreeDataStruct(data, 'id', 'parentId')
        this.listLoading = false
      })
    },
    getGoodsTypeValue(value) {
      this.goodsTypeValueId = value
      if (value) {
        this.query.goodsSpecification.goodsTypeId = value.id
      }
    },
    expandChange(row, rowExtended) {

      this.query.goodsSpecificationItem.specificationId = row.id
      this.query.goodsSpecificationItem.organId = row.organId || getOrganId()
      this.query.goodsSpecificationItem.organPath = row.organPath || getOrganPath()
      this.query.goodsSpecificationItem.tenantId = row.tenantId || getTenantId()
      this.handleQueryGoodsTypeSpecificationItem()
    },
    handleAssignGoodsSpecification(index, row) {
      this.dialogFormAssignGroup2OrganVisible = true
    },
    handleShowOrganList(index, row) {
      this.dialogStatus = 'assignOrgan'
      this.query.organ.organPath = row.organPath || getOrganPath
      getOrganPagerList(this.query.organ, this.query.organ.currentPage, this.query.organ.pageSize).then(response => {
        const data = response.data
        this.query.organ.total = data.totalCount
        this.query.organ.pageSize = data.pageSize
        this.query.organ.currentPage = data.currentPage
        this.organList = handleBuildTreeDataStruct(data.entities, 'id', 'parentId')
        this.listLoading = false
      })
    },
    handleShowAssignGroup2OrganShowOrganList(index, row) {
      this.dialogFormAssignGroup2OrganVisible = true
      this.assign2Organs.id = row.id
      this.handleShowOrganList(index, row);
    },
    handleShowAssignSpecification2OrganList(index, row) {
      this.dialogFormAssignSpecificationOrganVisible = true
      this.assignGoodsSpecification2Organs.id = row.id
      this.handleShowOrganList(index, row);
    },
    handleOrganSizeChange(val) {
      this.query.organ.currentPage = 1
      this.query.organ.pageSize = val
      this.handleShowOrganList()
    },
    handleOrganCurrentChange(val) {
      this.query.organ.currentPage = val
      this.handleShowOrganList()
    },
    handleOrganSelectionChange(val) {

      if (this.dialogFormAssignGroup2OrganVisible) {
        console.log(val)
        this.assign2Organs.assignees = val
        this.assign2Organs.createBy = getCurrentLoginUserId()
        this.assign2Organs.createDate = getCurrentTimeYYYY_MM_dd_HH_mm_ss()
        this.assign2Organs.updateBy = getCurrentLoginUserId()
      } else if (this.dialogFormAssignSpecificationOrganVisible) {
        this.assignGoodsSpecification2Organs.assignees = val
        this.assignGoodsSpecification2Organs.createDate = getCurrentTimeYYYY_MM_dd_HH_mm_ss()
        this.assignGoodsSpecification2Organs.createBy = getCurrentLoginUserId()
        this.assignGoodsSpecification2Organs.updateBy = getCurrentLoginUserId()
      }

    },
    handleAssignGoodsSpecificationGroup2OrganSave() {
      this.listLoading = true
      assignGoodsSpecificationGroup2Organ(this.assign2Organs).then(response => {
        const data = response.data
        if (data.successful) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.fetchData();
        } else {
          this.$message({
            type: 'info',
            message: data.description || getSystemErrorCode(data.status)
          })
        }
        this.listLoading = false
      })
    },
    handleGoodsSpecification2OrganSave() {
      this.listLoading = true
      assignGoodsSpecification2Organ(this.assignGoodsSpecification2Organs).then(response => {
        const data = response.data
        if (data.successful) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.fetchData();
        } else {
          this.$message({
            type: 'info',
            message: '操作失败!'
          })
        }
        this.listLoading = false
      })
    },
    getGoodsType(form) {
      let nodesInfo = this.$refs['goodsTypeTree'].getCheckedNodes()
      form.goodsTypeId = nodesInfo[0].data.id
      form.goodsTypePath = nodesInfo[0].data.typePath
    },
    initOrganTree(fn, filter, that) {
      this.listLoading = true
      let data = getOrganList()
      if (filter) {
        data = data.filter(function (item, index) {
          return item.organPath.indexOf(filter) >= 0
        })
      }
      if (that && that.organFormOptions && that.organFormOptions.length >= 0) {
        that.organFormOptions = handleBuildTreeDataStruct(data, 'id', 'parentId')
      } else {
        this.organOptions = handleBuildTreeDataStruct(data, 'id', 'parentId')
      }
      this.listLoading = false
      if (fn) {
        fn()
      }
    },
    initOrganFormTree(fn, filter) {
      this.initOrganTree(fn, filter, this)
    },
    setOrganQueryType() {
      this.organQueryModelChange(localStorageUtil.fetch("SYSTEM_SETTING_ORGAN_QUERY_MODEL"))
    },
    selectOrgan(nodes) {
      let nodesInfo = this.$refs['organTree'].getCheckedNodes()
      let that = this
      this.query.params.organPath = ''
      this.query.params.organIds = []
      if (this.props.multiple) {
        this.query.params.organIds = []
        if (nodesInfo.length) {
          nodesInfo.forEach(function (element) {
            that.query.params.organIds.push(element.data.id)
          })
        }
      } else if (nodesInfo.length) {
        this.query.params.organPath = nodesInfo[0].data.organPath
      }
    },
    selectTenant(nodes) {
      debugger
      let nodesInfo = this.$refs['tenantTree'].getCheckedNodes()
      let that = this
      this.query.params.tenantId = ''
      this.query.params.tenantIds = []
      this.query.params.organId = null
      this.query.params.organIds = []
      this.query.params.organPath = null

      if (this.tenantProps.multiple) {
        this.query.params.tenantIds = []
        if (nodesInfo.length) {
          nodesInfo.forEach(function (element) {
            that.query.params.tenantIds.push(element.data.id)
          })
        }
      } else if (nodesInfo.length) {
        this.query.params.tenantId = nodesInfo[0].data.id
        this.initOrganTree(function () {
          that.setOrganQueryType()
        }, nodesInfo[0].data.organPath)
      } else {
        this.initOrganTree(function () {
          that.setOrganQueryType()
        })
      }
    },
    chooseTenantForm(nodes) {
      let nodesInfo = this.$refs['tenantTreeForm'].getCheckedNodes()
      this.form.tenantId = null
      let that = this
      if (nodesInfo.length) {
        this.form.tenantId = nodesInfo[0].data.id
        this.initOrganFormTree(function () {
          that.setOrganQueryType()
        }, nodesInfo[0].data.organPath, this.organFormOptions)
      } else {
        this.initOrganFormTree(function () {
          that.setOrganQueryType()
        }, null, this.organFormOptions)
      }
    },
    organQueryModelChange(organQueryModel) {
      if (organQueryModel == 'selected') {
        this.props.multiple = true
      } else if (organQueryModel == 'selfAndSub') {
        this.props.multiple = false
      }
    },
    chooseOrganForm(nodes) {
      let nodesInfo = this.$refs['organTreeForm'].getCheckedNodes()
      if (nodesInfo[0]) {
        this.form.organPath = nodesInfo[0].data.organPath
        this.form.organId = nodesInfo[0].data.id
        this.form.tenantId = nodesInfo[0].data.tenantId
      }
    },
    handleFiltTenant() {
      let data = getOrganList()
      this.tenantOptions = data.filter(function (item, index) {
        return item.type == 0 || item.type == 1;
      })
    },
    handleFiltSupplier() {
      let data = getOrganList()
      this.supplierOptions = data.filter(function (item, index) {
        return item.type == 99;
      })
    },
    initGoodsTypeTree() {
      this.listLoading = true
      getGoodsTypeTree({ organId: getOrganId() }).then(response => {
        console.log(response)
        const data = response.data.data
        this.goodsTypeTreeOptionData = handleBuildTreeDataStruct(data, 'id', 'parentId')
        this.listLoading = false
      })
    },
  }
}

</script>
